package com.lie.prepare.basic;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by lie on 2018/4/10.
 */

class Task implements Runnable{
    private static int counter = 0;
//    private final int id = counter++;
    private int id = counter++;

    private static int counterB = 0;

    public void run() {
        System.out.println(this + " id = "+ id);
        System.out.println(this + " counterB = "+ counterB++);
    }
}

class MakeTask implements Callable<Task>{

    public Task call() throws Exception {
        return new Task();
    }
}

public class RunnableStaticDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        createByMain();

//        createByExe();
    }

    private static void createByExe() throws InterruptedException, ExecutionException {
        ExecutorService exec = Executors.newCachedThreadPool();
        List<Task> taskList = new ArrayList<Task>();
        for (int i = 0; i < 100; i++) {
            Task task = exec.submit(new MakeTask()).get();
            taskList.add(task);
        }

        for (Task task: taskList){
            exec.execute(task);
        }

        exec.shutdown();
    }

    //全部任务thread由main创建
    private static void createByMain() {
        ExecutorService exec = Executors.newCachedThreadPool();
//        for (int i = 0; i < 10; i++) {
//            exec.execute(new Task());
//        }

        Task task = new Task();
        for (int i = 0; i < 10; i++) {
            exec.execute(task);
            Thread.yield();
        }
        exec.shutdown();
    }

}




